﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;


namespace Qiang.Controls
{
    [Serializable]
    public class MapFunctionPan : MapFunction
    {
        public MapFunctionPan()
            : base()
        {
            MemoryStream stream = new MemoryStream(Images.cursorHand);
            Cursor = new System.Windows.Forms.Cursor(stream);
        }
             

        private bool _isDragging = false;

        private Point _dragStart;

        private Rectangle _oldImageView;

        Rectangle _currentView;


        protected override void OnMouseDown(MapMouseArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left) return;

            this._isDragging = true;

            this._dragStart = e.Location;

            this._oldImageView = e.Control.Map.CurrentView;

            base.OnMouseDown(e);
        }


        protected override void OnMouseMove(MapMouseArgs e)
        {
            if (this._isDragging)
            {
                int disX = e.Location.X - this._dragStart.X;
                int disY = e.Location.Y - this._dragStart.Y;

                _currentView = new Rectangle(this._oldImageView.X - disX, this._oldImageView.Y - disY,
                     this._oldImageView.Width, this._oldImageView.Height);
                e.Control.Map.CurrentView = _currentView;
                this.MapControl.Invalidate();
            }

            base.OnMouseMove(e);
        }

        protected override void OnMouseUp(MapMouseArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left) return;

            if (this._isDragging)
            {
                this._isDragging = false;
            }

            e.Control.Map.ResetExtent(_currentView);

            this.MapControl.Invalidate();
            
            base.OnMouseUp(e);
        }



    }
}
